//查询地址余额
let getBalance=(address,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=account&action=balance&address=${address}&tag=latest&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}
//查询地址所有交易
let getAllTransaction=(address,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=account&action=txlist&address=${address}&startblock=0&endblock=99999999&sort=asc&apikey=${apikey}`)
        .then(res => {
          console.log(res.json())
            return res.json();
        })
}
//查询某笔交易信息
let getTransactionByHash=(txhash,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=proxy&action=eth_getTransactionByHash&txhash=${txhash}&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}
//查询地址所有ERC20代币交易
let getAllERC20Transaction=(address,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=account&action=tokentx&address=${address}&startblock=0&endblock=999999999&sort=asc&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}
//查询地址某个ERC20代币所有交易
let getSingerERC20Transaction=(contractAddress,address,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=account&action=tokentx&contractaddress=${contractAddress}&address=${address}&page=1&offset=100&sort=asc&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}

//查询某个地址交易总数量
let getTransactionInfo=(address,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=proxy&action=eth_getTransactionCount&address=${address}&tag=latest&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}
//获取当前油价
let getGasPrice=(apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=proxy&action=eth_gasPrice&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}

//查询ERC20 totalSupply
let getErc20ToralSupply=(contractAddress,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=stats&action=tokensupply&contractaddress=${contractAddress}&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}

//查询账户ERC20令牌余额
let getAddressErc20Balance=(contractAddress,address,apikey)=>{
    fetch(`http://api-cn.etherscan.com/api?module=account&action=tokenbalance&contractaddress=${contractAddress}&address=${address}&tag=latest&apikey=${apikey}`)
        .then(res => {
            return res.json();
        })
}
module.exports={
    getBalance,
    getAllTransaction,
    getTransactionByHash,
    getAllERC20Transaction,
    getSingerERC20Transaction,
    getTransactionInfo,
    getGasPrice,
    getErc20ToralSupply,
    getAddressErc20Balance
};
